﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BookStore.Models;

namespace BookStore.Controllers
{
    public class HomeController : Controller
    {
        public MVC3BookStoreEntities BookStoreEntities { get; set; }
        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            if (BookStoreEntities == null) BookStoreEntities = new MVC3BookStoreEntities();
            base.Initialize(requestContext);
        }

        public ActionResult Index()
        {
            var Books = BookStoreEntities.Books.ToList().Where(B => B.PublishingYear >= DateTime.Now.Year - 1 );
            ViewBag.Promotions = from book in BookStoreEntities.Books
                                 join Per in BookStoreEntities.Promotions on book.ID equals Per.BookID
                                 select book;
            return View(Books);
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult Detail(int? ItemID)
        {
            var Item = (from Bk in BookStoreEntities.Books
                       where Bk.ID == ItemID.Value
                       select Bk).First();

            ViewBag.Item = Item;
            return View();
        }
    }
}
